setwd('~/LGG/sankey')
dir.create('results')

library(ggplot2)
library(ggalluvial)
data <-read.csv("sankey.csv",header=T,sep=",",fileEncoding="gbk") #导入数据
data
# 假设PRG列有5个唯一值，您可以定义如下颜色向量
colors <- grDevices::colorRampPalette(c("#F66F69", "#FEB3AE", 
                                        "#E5855D", "#1597A5", 
                                        "#0E606B","#FFC24B"))(106)

df <- to_lodes_form(data,
                    key = "x", value = "stratum", id = "alluvium",
                    axes = 1:3)
ggplot_chart<-ggplot(df, aes(x = x, y=Value, fill=stratum, label=stratum,
               stratum = stratum, alluvium  = alluvium))+#数据
  geom_flow()+
  geom_stratum(width = 0.28, alpha=0.7)+#图中方块的宽度
  geom_text(stat = 'stratum', size = 1, color = 'black')+
  scale_fill_manual(values = colors)+#自定义颜色
  theme_void()+#主题（无轴及网格线）
  theme(legend.position = 'none')#去除图例

ggsave(filename = "results/sankey.pdf",  # 文件名和格式
       plot = ggplot_chart,  # 指定要保存的图表
       width = 6,  # 图像宽度（英寸）
       height = 9)  # 分辨率

